<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Api\Transfer;
use App\Repository\UpThree\GetThreeOrderRepostition;
use App\Api\CatchOrder\DouYin;
use App\Repository\Statist\OrderStatistRepository;
use  App\Repository\Order\OrderInfoRepository as infoModel;
use App\Repository\Orders\OrdersRepository;
// 分销商
class CountOrderController extends BaseController
{
  private $repoist;
  private $infoModel;
  public function __construct()
  {
    $this->repoist = new OrderStatistRepository();
    $this->infoModel = new infoModel();
  }
  
  
  public function getStatistics(Request $request)
  {
    // var_dump($request->all(), 11111111);
    $params = $request->all();
    $where = [];
    if (empty($params['shop_id'])) {
      $where[] = ['orders.shop_id', '=', $request->user->shop_id];
      if (!empty($params['goods_id'])) {
        $where[] = ['orders.goods_id', '=', $params['goods_id']];
      }
    }
  
    if (!empty($params['source'])) {
      $where[] = ['user.source', '=', $params['source']];
    }
    if (!empty($params['shop_id']) && !empty($params['goods_id'])) {
      $where[] = ['sup_goods_id_str', 'like', $params['goods_id']];
    };
  
    $timeStart = date('Y-m-d 00:00:00');
    $timeEnd = date('Y-m-d 23:59:59');
    if (!empty($params['dateRange'])) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($params['dateRange'][0]));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($params['dateRange'][1]));
    }
  
    // var_dump($where);
    $timeArr = [];
    $start = strtotime($timeStart);
    // var_dump($start);
    $end = strtotime($timeEnd);
    // var_dump($end);
    while ($end >= $start) {
      $timeArr[] = date('Y-m-d', $start);
      $start = $start + 86400;
    }
    // var_dump($timeArr);
    // 查询数据
    $datasOrder = [];  // 订单数据
    $datasDev = []; // 发货数据
    $actDatas = []; // 激活数据
    $failDatas = []; // 失败数据
    $dataStatus = [
      0,
      20,
      40,
      60
    ];
   
    $twhere=$where;
  
    $twhere[] = ['orders.created_at', '>=', $timeStart];
    $twhere[] = ['orders.created_at', '<=', $timeEnd];
  
    $td=strtotime(date('Y-m-d'));
    $yes_start = date('Y-m-d 23:59:59',$td-86400);
    $yes_end = date('Y-m-d 00:00:00');
    //总数
    $total=DB::table('orders')->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->where($twhere)->count();
  
    $yes_total=DB::table('orders')
    ->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')
    ->where($twhere)
    ->where('orders.created_at','>=',$yes_start)
    ->where('orders.created_at','<=',$yes_end)
    ->count();
  
  
  
    //发货数量
    $swhere=$where;
    $swhere[] = ['orders.status', '=', 20];
  
    $stotal=DB::table('orders')
            ->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')
            ->where($swhere)->count();
    $yes_stotal=DB::table('orders')
                ->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')
                ->where($swhere)
                ->where('orders.created_at','>=',$yes_start)
                ->where('orders.created_at','<=',$yes_end)
                ->count();
    //激活
    $awhere=$where;
    $awhere[] = ['orders.status', '=', 40];
    $atotal=DB::table('orders')->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->where($awhere)->count();
    $yes_atotal=DB::table('orders')
    ->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')
    ->where($awhere)
    ->where('orders.created_at','>=',$yes_start)
    ->where('orders.created_at','<=',$yes_end)
    ->count();
  
    //失败
    $fwhere=$where;
    $fwhere[] = ['orders.status', '=', 60];
    $ftotal=DB::table('orders')->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->where($fwhere)->count();
    $yes_ftotal=DB::table('orders')
    ->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')
    ->where($fwhere)
    ->where('orders.created_at','>=',$yes_start)
    ->where('orders.created_at','<=',$yes_end)
    ->count();
  
  
    $data = [
      'total' => [
          'total'=>$total,
          'yes_total'=>$yes_total,
      ],
      'send' => [
          'total'=>$stotal,
          'yes_total'=>$yes_stotal,
      ],
      'active' => [
          'total'=>$atotal,
          'yes_total'=>$yes_atotal,
      ],
      'fails' => [
          'total'=>$ftotal,
          'yes_total'=>$yes_ftotal,
      ],
  
    ];
    return $this->returnMsg(200, '查询成功', $data);
  }
  
  
  public function getOrderDatas(Request $request)
  {
    // var_dump($request->all(), 11111111);
    $params = $request->all();
    $where = [];
    if (empty($params['shop_id'])) {
      $where[] = ['orders.shop_id', '=', $request->user->shop_id];
      if (!empty($params['goods_id'])) {
        $where[] = ['orders.goods_id', '=', $params['goods_id']];
      }
    }
  
    if (!empty($params['source'])) {
      $where[] = ['user.source', '=', $params['source']];
    }
    if (!empty($params['shop_id']) && !empty($params['goods_id'])) {
      $where[] = ['sup_goods_id_str', 'like', $params['goods_id']];
    };
  
    $timeStart = date('Y-m-d 00:00:00');
    $timeEnd = date('Y-m-d 23:59:59');
    if (!empty($params['dateRange'])) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($params['dateRange'][0]));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($params['dateRange'][1]));
    }
  
    // var_dump($where);
    $timeArr = [];
    $start = strtotime($timeStart);
    // var_dump($start);
    $end = strtotime($timeEnd);
    // var_dump($end);
    while ($end >= $start) {
      $timeArr[] = date('Y-m-d', $start);
      $start = $start + 86400;
    }
    // var_dump($timeArr);
    // 查询数据
    $datasOrder = [];  // 订单数据
    $datasDev = []; // 发货数据
    $actDatas = []; // 激活数据
    $failDatas = []; // 失败数据
    $dataStatus = [
      0,
      20,
      40,
      60
    ];
  
    // {id: 0, value: "提交中"}
    // {id: 1, value: "提交上游"}
    // {id: 3, value: "需上传证件照"}
    // {id: 10, value: "审核中"}
    // {id: 11, value: "待发货"}
    // {id: 20, value: "已发货"}
    // {id: 30, value: "已签收"}
    // {id: 40, value: "已激活"}
    // {id: 50, value: "异常单"}
    // {id: 51, value: "取消单"}
    // {id: 60, value: "失败单"}
  
    $twhere=$where;
  
    $twhere[] = ['orders.created_at', '>=', $timeStart];
    $twhere[] = ['orders.created_at', '<=', $timeEnd];
  
    $td=strtotime(date('Y-m-d'));
    $yes_start = date('Y-m-d 23:59:59',$td-86400);
    $yes_end = date('Y-m-d 00:00:00');
    //总数
    $total=DB::table('orders')->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->where($where)->count();
  
    //已审核
    $cwhere=$where;
    $cwhere[] = ['orders.status', '=', 11];
  
    $ctotal=DB::table('orders')
            ->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')
            ->where($cwhere)->count();
  
  
    //发货数量
    $swhere=$where;
    $swhere[] = ['orders.status', '=', 20];
  
    $stotal=DB::table('orders')
            ->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')
            ->where($swhere)->count();
  
    //激活
    $awhere=$where;
    $awhere[] = ['orders.status', '=', 40];
    $atotal=DB::table('orders')->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->where($awhere)->count();
  
    //失败
    $fwhere=$where;
    $fwhere[] = ['orders.status', '=', 60];
    $ftotal=DB::table('orders')->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->where($fwhere)->count();
  
  
  
    $data = [
      'total' =>$total,
      'check' =>$ctotal,
      'send' => $stotal,
      'active' => $atotal,
      'fails' => $ftotal,
    ];
    return $this->returnMsg(200, '查询成功', $data);
  }
  

  public function getDatas(Request $request)
  {
    // var_dump($request->all(), 11111111);
    $params = $request->all();
    $where = [];
    if (empty($params['shop_id'])) {
      $where[] = ['info.shop_id', '=', $request->user->shop_id];
      if (!empty($params['goods_id'])) {
        $where[] = ['order.goods_id', '=', $params['goods_id']];
      }
    }
    if (!empty($params['source'])) {
      $where[] = ['info.source', '=', $params['source']];
    }
    if (!empty($params['shop_id']) && !empty($params['goods_id'])) {
      $where[] = ['sup_goods_id_str', 'like', $params['goods_id']];
    };
    $timeStart = date('Y-m-d 00:00:00');
    $timeEnd = date('Y-m-d 23:59:59');
    if (!empty($params['dateRange'])) {
      $timeStart = date('Y-m-d 00:00:00', strtotime($params['dateRange'][0]));
      $timeEnd = date('Y-m-d 23:59:59', strtotime($params['dateRange'][1]));
    }

    // var_dump($where);
    $timeArr = [];
    $start = strtotime($timeStart);
    // var_dump($start);
    $end = strtotime($timeEnd);
    // var_dump($end);
    while ($end >= $start) {
      $timeArr[] = date('Y-m-d', $start);
      $start = $start + 86400;
    }
    // var_dump($timeArr);
    // 查询数据
    $datasOrder = [];  // 订单数据
    $datasDev = []; // 发货数据
    $actDatas = []; // 激活数据
    $failDatas = []; // 失败数据
    $dataStatus = [
      0,
      20,
      40,
      60
    ];
    if (!empty($timeArr)) {
      foreach ($timeArr as $key => $v) {
        // $whereData = [];
        $startTimeOne = date('Y-m-d 00:00:00', strtotime($v));
        $entTimeOne = date('Y-m-d 23:59:59', strtotime($v));
        // $whereData[] = [];
        $timeWhere = $where;
        $timeWhere[] = ['info.created_at', '>=', $startTimeOne];
        $timeWhere[] = ['info.created_at', '<=', $entTimeOne];
        // $this->infoModel->getModel()->where($where)->count();
        // DB::connection()->enableQueryLog();
        $datasOrder[] =  DB::table('order_info as info')->leftJoin('order', 'order.id', '=', 'info.order_id')->where($timeWhere)->count();
        // $logs = DB::getQueryLog();
        // var_dump($logs);
        $whereactDatas = $timeWhere; //  激活
        $whereactDatas[] = ['info.status', '=', 40];
        // var_dump($whereactDatas, 111111);
        $actDatas[] =  DB::table('order_info as info')->leftJoin('order', 'order.id', '=', 'info.order_id')->where($whereactDatas)->count();
        // $logs = DB::getQueryLog();
        // var_dump($logs);
        // $actDatas[] = $this->infoModel->getModel()->where($whereactDatas)->count();
        $wheredatasDev = $timeWhere;  // 发货
        $wheredatasDev[] = ['info.status', '=', 20];
        // var_dump($wheredatasDev, 2222222222);
        $datasDev[] =  DB::table('order_info as info')->leftJoin('order', 'order.id', '=', 'info.order_id')->where($wheredatasDev)->count();
        // $logs = DB::getQueryLog();
        // var_dump($logs);
        // $datasDev[] = $this->infoModel->getModel()->where($wheredatasDev)->count();
        $wherefailDatas = $timeWhere;  // 失败
        $wherefailDatas[] = ['info.status', '=', 60];
        // var_dump($wherefailDatas, 333333333);
        $failDatas[] =  DB::table('order_info as info')->leftJoin('order', 'order.id', '=', 'info.order_id')->where($wherefailDatas)->count();
        // $logs = DB::getQueryLog();
        // var_dump($logs);
        // $failDatas[] = $this->infoModel->getModel()->where($wherefailDatas)->count();
      }
    }
    $data = [
      'field' => $timeArr,
      'order' => $datasOrder, //订单数据
      'dev' => $datasDev, // 发货数据
      'act' => $actDatas, // 激活数据
      'fail' => $failDatas, // 失败数据
    ];
    return $this->returnMsg(200, '查询成功', $data);
  }
  public function getOneDistAndProduct(Request $request)
  {
    $params = $request->all();
    $where = [];
    $shop_id = $request->user->shop_id; // 店铺id
    $dist_shop_id = $shop_id; // 代理id
    if (!empty($params['shop_id'])) {
      $dist_shop_id = $params['shop_id'];
    }
    $time = date('Y-m-d');
    $startTime = date('Y-m-d 00:00:00');
    $endTime = date('Y-m-d 23:59:59');
    if (!empty($params['dateRange'])) {
      $time = $params['dateRange'];
      $startTime = $time[0];
      $endTime = $time[1];
    }
    $timeWhere = [];
    $timeWhere[] = [
      'orders.created_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
    ];
    $timeWhere[] = [
      'orders.created_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
    ];
    // 激活时间
    if (!empty($params['timeType']) && $params['timeType'] == 2) {
      $timeWhere = [];
      $timeWhere[] = [
        'orders.activat_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
      ];
      $timeWhere[] = [
        'orders.activat_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
      ];
    }
    // 发货时间
    if (!empty($params['timeType']) && $params['timeType'] == 1) {
      $timeWhere = [];
      $timeWhere[] = [
        'orders.deliver_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
      ];
      $timeWhere[] = [
        'orders.deliver_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
      ];
    }
    if (!empty($params['channel_id'])) {
      $timeWhere[] = ['orders.sup_id', '=', $params['channel_id']];
    }
    if (!empty($params['source'])) {
      $timeWhere[] = ['orders_user.source', '=', $params['source']];
    }
    $arrayGoods = [];
    // $arrayData = DB::Table('order')->select(DB::raw('count(*) as nums,status,goods_id,goods_name,goods_code'))->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
    //   $actquery->where('shop_id', '=', $dist_shop_id);
    //   if ($dist_shop_id != $shop_id) {
    //     $actquery->OrWhere('shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
    //   } else {
    //     $actquery->OrWhere('shop_id_str', 'like', '%.' . $dist_shop_id . '.%');
    //   }
    // })->groupBy('goods_id', 'goods_name', 'status', 'goods_code')->orderBy('goods_id', 'asc')->get();
    // DB::connection()->enableQueryLog();
    $arrayData = DB::Table('orders')->select(DB::raw('count(*) as number,status,one_product_id,one_product_name,one_product_code,sup_product_id,sup_product_name,sup_product_code,product_id,product_name,product_code,nums as type'))->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
      $actquery->where('orders.shop_id', '=', $dist_shop_id);
      if ($dist_shop_id != $shop_id) {
        $actquery->OrWhere('orders.shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
      } else {
        $actquery->OrWhere('orders.shop_id_str', 'like',  $dist_shop_id . '.%');
      }
    })->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->groupBy('status', 'one_product_id', 'one_product_name', 'one_product_code', 'sup_product_id', 'sup_product_name', 'sup_product_code', 'product_id', 'product_name', 'product_code', 'nums')->orderBy('sup_product_id', 'asc')->orderBy('nums', 'asc')->get();
    // 统计数据
    // $log = DB::getQueryLog();
    // var_dump($log);
    if (!empty($arrayData)) {
      // 重新组装数组
      $productArr = [];
      foreach ($arrayData as $key => $v) {
        // 数据转发
        $product_code_key = $v->one_product_code;
        $product_code_two_key = $v->product_code;
        if ($v->type == 1) {
          $productArr[$product_code_key][$product_code_two_key][] = $v; // 第一个
        }
        if ($v->type != 1) {
          $productArr[$product_code_key]['child'][$product_code_two_key][] = $v; // 子节点
        }
      }
    }
    $id = 0;
    if (!empty($productArr)) {
      $productArrRes = []; // 需要输入的数据
      if (!empty($productArr)) {
        $totleNumsAll = 0; // 总数
        $subNumsAll = 0;
        $fahuoNumsAll = 0;
        $jihuoNumsAll = 0;
        $failedNumsAll = 0;
        $totleNumsAllChild = 0; // 总数 转单总数
        $subNumsAllChild = 0;
        $fahuoNumsAllChild = 0;
        $jihuoNumsAllChild = 0;
        $failedNumsAllChild = 0;
        foreach ($productArr as $key => $vval) {
          $arrays = [];
          $name = [];
          foreach ($vval as $ks => $vs) {
            $id++;
            if ($ks != 'child') {
              // 原始套餐
              $arrays = $this->oneArrCount($vs);
              // var_dump($vs);
              // $arrays['name'] = $ks;
              $arrays['id'] = $id;
              $arrays['hasChildren'] = false;
              $totleNumsAll += $arrays['name1'];
              $subNumsAll += $arrays['name2'];
              $fahuoNumsAll += $arrays['name3'];
              $jihuoNumsAll += $arrays['name4'];
              $failedNumsAll += $arrays['name5'];
            }
            if ($ks == 'child') {
              if (!empty($vs)) {
                foreach ($vs as $keys => $v) {
                  // var_dump($vs);
                  $id++;
                  $blas = $this->oneArrCount($v);
                  // $blas['name'] = $v['product_name'];
                  $blas['id'] = $id;
                  $array['hasChildren'] = true;
                  $totleNumsAllChild += $blas['name1'];
                  $subNumsAllChild += $blas['name2'];
                  $fahuoNumsAllChild += $blas['name3'];
                  $jihuoNumsAllChild += $blas['name4'];
                  $failedNumsAllChild += $blas['name5'];
                  if (!empty($arrays)) {
                    $arrays['child'][] = $blas;
                  }
                  if (empty($arrays)) {
                    $arrays = $blas;
                  }
                }
              }

              // $array = $this->oneArrCount($vval);
            }
          }
          // var_dump($arrays);
          $arrayGoods[] = $arrays;
        }
        // 总量
        $array = [];
        $array['name1'] = $totleNumsAll;
        $array['name2'] = $subNumsAll;
        $array['name3'] = $fahuoNumsAll;
        $array['name4'] = $jihuoNumsAll;
        $array['name5'] = $failedNumsAll;
        $array['name10'] = 0;
        $array['name41'] = 0;
        $array['name40'] = 0;
        $array['name20'] = 0;
        if (!empty($totleNumsAll)) {
          $array['name10'] = (round(($fahuoNumsAll / $totleNumsAll), 4) * 100) . '%';
          $array['name41'] = (round(($jihuoNumsAll / $totleNumsAll), 4) * 100) . '%';
          $array['name20'] = (round(($subNumsAll / $totleNumsAll), 4) * 100) . '%';
        }
        if (!empty($fahuoNumsAll)) {
          $array['name40'] = (round(($jihuoNumsAll / $fahuoNumsAll), 4) * 100) . '%';
        }
        $array['name'] = '统计';
        $id++;
        $array['id'] = $id;
        $array['child'][0]['name'] = '转单统计';
        $array['child'][0]['name1'] = $totleNumsAllChild;
        $array['child'][0]['name2'] = $subNumsAllChild;
        $array['child'][0]['name3'] = $fahuoNumsAllChild;
        $array['child'][0]['name4'] = $jihuoNumsAllChild;
        $array['child'][0]['name5'] = $failedNumsAllChild;
        $array['child'][0]['name10'] = 0;
        $array['child'][0]['name41'] = 0;
        $array['child'][0]['name40'] = 0;
        $array['child'][0]['name20'] = 0;
        $id++;
        $array['child'][0]['id'] = $id;
        if (!empty($totleNumsAllChild)) {
          $array['child'][0]['name10'] = (round(($fahuoNumsAllChild / $totleNumsAllChild), 4) * 100) . '%';
          $array['child'][0]['name41'] = (round(($jihuoNumsAllChild / $totleNumsAllChild), 4) * 100) . '%';
          $array['child'][0]['name20'] = (round(($subNumsAllChild / $totleNumsAllChild), 4) * 100) . '%';
        }
        if (!empty($fahuoNumsAllChild)) {
          $array['child'][0]['name40'] = (round(($jihuoNumsAllChild / $fahuoNumsAllChild), 4) * 100) . '%';
        }
        $arrayGoods[] = $array;
      }
    }
    // var_dump($arrayGoods, 11111111);
    // $keys = array_column($arrayGoods, 'name1');
    // array_multisort($arrayGoods, SORT_DESC, $keys);
    $data = [
      'product' => [
        ['label' => '商品名称', 'prop' => 'name'],
        ['label' => '入库数', 'prop' => 'name1'],
        ['label' => '提交运营商', 'prop' => 'name2'],
        ['label' => '提交运营商率', 'prop' => 'name20'],
        ['label' => '发货数量', 'prop' => 'name3'],
        ['label' => '发货率', 'prop' => 'name10'],
        ['label' => '激活数量', 'prop' => 'name4'],
        ['label' => '发货激活率', 'prop' => 'name40'],
        ['label' => '订单激活率', 'prop' => 'name41'],
        ['label' => '失败数量', 'prop' => 'name5'],
      ],
      'datas' => $arrayGoods,
    ];
    return $this->returnMsg(200, '查询成功', $data);
  }
  public function oneArrCount($arrays)
  {
    if (!empty($arrays)) {
      $totleNums = 0; //总数
      $subNums = 0;  // 提交上游的数量
      $fahuoNums = 0; // 发货数量
      $jihuoNums = 0; // 激活数量
      $failedNums = 0;
      $array = [];
      $name = '';
      foreach ($arrays as $key => $v) {
        $totleNums += $v->number;
        if ($v->status > 0) {
          $subNums += $v->number;
        }
        if ($v->status >= 20 && $v->status <= 40) {
          $fahuoNums += $v->number;
        }
        if ($v->status == 40) {
          $jihuoNums += $v->number;
        }
        if ($v->status > 40) {
          $failedNums += $v->number;
        }
        $name = $v->product_name;
      }
      $array = [];
      $array['name1'] = $totleNums;
      $array['name2'] = $subNums;
      $array['name3'] = $fahuoNums;
      $array['name4'] = $jihuoNums;
      $array['name5'] = $failedNums;
      $array['name10'] = 0;
      $array['name41'] = 0;
      $array['name40'] = 0;
      $array['name20'] = 0;
      if (!empty($totleNums)) {
        $array['name10'] = (round(($fahuoNums / $totleNums), 4) * 100) . '%';
        $array['name41'] = (round(($jihuoNums / $totleNums), 4) * 100) . '%';
        $array['name20'] = (round(($subNums / $totleNums), 4) * 100) . '%';
      }
      if (!empty($fahuoNums)) {
        $array['name40'] = (round(($jihuoNums / $fahuoNums), 4) * 100) . '%';
      }
      $array['name'] = $name;
      return $array;
    }
  }
  // 更改日期 1月份 换成新的的信息
  public function getOneDistAndProduct_back2(Request $request)
  {
    $params = $request->all();
    $where = [];
    $shop_id = $request->user->shop_id;
    $dist_shop_id = $shop_id;
    // if (empty($params['shop_id'])) {
    //   $where[] = ['shop_id', '=', ];
    // }


    if (!empty($params['shop_id'])) {
      $dist_shop_id = $params['shop_id'];
    }
    $time = date('Y-m-d');
    $startTime = date('Y-m-d 00:00:00');
    $endTime = date('Y-m-d 23:59:59');
    // $timeEnd = date('Y-m-d 23:59:59');
    if (!empty($params['dateRange'])) {
      // $time = date('Y-m-d', strtotime($params['dateRange']));
      $time = $params['dateRange'];
      $startTime = $time[0];
      $endTime = $time[1];
      // $timeEnd = date('Y-m-d 23:59:59', strtotime($params['dateRange'][1]));
    }
    $timeWhere = [];
    $timeWhere[] = [
      // 'created_at', '>=', date('Y-m-d 00:00:00', strtotime($time)),
      'created_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
    ];
    $timeWhere[] = [
      'created_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
    ];
    if (!empty($params['channel_id'])) {
      $timeWhere[] = ['sup_id', '=', $params['channel_id']];
    }
    if (!empty($params['source'])) {
      $timeWhere[] = ['source', '=', $params['source']];
    }

    $goodsWhere = [
      'shop_id' => $request->user->shop_id,
      'status' => 0
    ];
    // $goodsArr = DB::table('goods')->where($goodsWhere)->select(['name', 'id'])->get();
    $totle = 0;
    $arrayGoods = [];
    $arrayData = DB::Table('order')->select(DB::raw('count(*) as nums,status,goods_id,goods_name,goods_code'))->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
      $actquery->where('shop_id', '=', $dist_shop_id);
      if ($dist_shop_id != $shop_id) {
        $actquery->OrWhere('shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
      } else {
        $actquery->OrWhere('shop_id_str', 'like', '%.' . $dist_shop_id . '.%');
      }
    })->groupBy('goods_id', 'goods_name', 'status', 'goods_code')->orderBy('goods_id', 'asc')->get();
    // $logs = DB::getQueryLog();
    // var_dump($logs);
    // die();
    if (!empty($arrayData)) {

      $goodsArr = [];
      foreach ($arrayData as $key => $v) {
        $goods_id = $v->goods_code;
        $goodsArr[$goods_id][] = $v;
      }
      // var_dump($goodsArr);
      if (!empty($goodsArr)) {
        $totleNumsAll = 0;
        $subNumsAll = 0;
        $fahuoNumsAll = 0;
        $jihuoNumsAll = 0;
        $failedNumsAll = 0;
        foreach ($goodsArr as $vval) {
          $totleNums = 0; //总数
          $subNums = 0;  // 提交上游的数量
          $fahuoNums = 0; // 发货数量
          $jihuoNums = 0; // 激活数量
          $failedNums = 0;
          if (!empty($vval)) {
            foreach ($vval as $key => $v) {
              $totleNums += $v->nums;
              $totleNumsAll += $v->nums;
              if ($v->status > 0) {
                $subNums += $v->nums;
                $subNumsAll += $v->nums;
              }
              if ($v->status >= 20 && $v->status <= 40) {
                $fahuoNums += $v->nums;
                $fahuoNumsAll += $v->nums;
              }
              if ($v->status == 40) {
                $jihuoNums += $v->nums;
                $jihuoNumsAll += $v->nums;
              }
              if ($v->status > 40) {
                $failedNums += $v->nums;
                $failedNumsAll += $v->nums;
              }
            }
          }
          $array['name1'] = $totleNums;
          $array['name2'] = $subNums;
          $array['name3'] = $fahuoNums;
          $array['name4'] = $jihuoNums;
          $array['name5'] = $failedNums;
          $array['name10'] = 0;
          $array['name41'] = 0;
          $array['name40'] = 0;
          $array['name20'] = 0;
          if (!empty($totleNums)) {
            $array['name10'] = (round(($fahuoNums / $totleNums), 4) * 100) . '%';
            $array['name41'] = (round(($jihuoNums / $totleNums), 4) * 100) . '%';
            $array['name20'] = (round(($subNums / $totleNums), 4) * 100) . '%';
          }
          if (!empty($fahuoNums)) {
            $array['name40'] = (round(($jihuoNums / $fahuoNums), 4) * 100) . '%';
          }
          $array['name'] = $v->goods_name;
          $arrayGoods[] = $array;
        }
        // 总量
        $array = [];
        $array['name1'] = $totleNumsAll;
        $array['name2'] = $subNumsAll;
        $array['name3'] = $fahuoNumsAll;
        $array['name4'] = $jihuoNumsAll;
        $array['name5'] = $failedNumsAll;
        $array['name10'] = 0;
        $array['name41'] = 0;
        $array['name40'] = 0;
        $array['name20'] = 0;
        if (!empty($totleNumsAll)) {
          $array['name10'] = (round(($fahuoNumsAll / $totleNumsAll), 4) * 100) . '%';
          $array['name41'] = (round(($jihuoNumsAll / $totleNumsAll), 4) * 100) . '%';
          $array['name20'] = (round(($subNumsAll / $totleNumsAll), 4) * 100) . '%';
        }
        if (!empty($fahuoNumsAll)) {
          $array['name40'] = (round(($jihuoNumsAll / $fahuoNumsAll), 4) * 100) . '%';
        }
        $array['name'] = '统计';
        $arrayGoods[] = $array;
      }
    }
    // foreach ($goodsArr as $k => $val) {
    //   // DB::connection()->enableQueryLog();

    //   // $tileName1 += $totleNums;

    // }
    // if (!empty($goodsArr)) {
    //   $goodsArr = json_decode(json_encode($goodsArr), true);
    //   $tileName1 = 0;

    //   $totle += $tileName1;
    // }
    // $arrayGoods('name1');
    $keys = array_column($arrayGoods, 'name1');
    array_multisort($arrayGoods, SORT_DESC, $keys);
    $data = [
      'product' => [
        ['label' => '商品名称', 'prop' => 'name'],
        ['label' => '入库数', 'prop' => 'name1'],
        ['label' => '提交运营商', 'prop' => 'name2'],
        ['label' => '提交运营商率', 'prop' => 'name20'],
        ['label' => '发货数量', 'prop' => 'name3'],
        ['label' => '发货率', 'prop' => 'name10'],
        ['label' => '激活数量', 'prop' => 'name4'],
        ['label' => '发货激活率', 'prop' => 'name40'],
        ['label' => '订单激活率', 'prop' => 'name41'],
        ['label' => '失败数量', 'prop' => 'name5'],
      ],
      'datas' => $arrayGoods,
    ];
    // $data['product'] = array_merge($data['product'], $arrayProduct);
    return $this->returnMsg(200, '查询成功', $data);
    $data = [
      'product' => [
        ['label' => '代理名称', 'prop' => 'name'],
        ['label' => '合计', 'prop' => 'name1'],
        ['label' => '产品1', 'prop' => 'name2'],
      ],
      'datas' => [
        ['name' => '商品1', 'name1' => 5, 'name2' => 6]
      ],
    ];
    return $this->returnMsg(200, '查询成功', $data);
  }
  // 产品代理数据
  public function getOneDistAndProduct_back(Request $request)
  {
    $params = $request->all();
    $where = [];
    $shop_id = $request->user->shop_id;
    // if (empty($params['shop_id'])) {
    //   $where[] = ['shop_id', '=', ];
    // }
    if (!empty($params['source'])) {
      $where[] = ['source', '=', $params['source']];
    }
    $time = date('Y-m-d');
    // $timeEnd = date('Y-m-d 23:59:59');
    if (!empty($params['dateRange'])) {
      $time = date('Y-m-d', strtotime($params['dateRange']));
      // $timeEnd = date('Y-m-d 23:59:59', strtotime($params['dateRange'][1]));
    }
    $timeWhere = [];
    $timeWhere[] = [
      'created_at', '>=', date('Y-m-d 00:00:00', strtotime($time)),
    ];
    $timeWhere[] = [
      'created_at', '<=', date('Y-m-d 23:59:59', strtotime($time)),
    ];

    // var_dump($time);
    // 查询出来所有的代理
    if (empty($params['shop_id'])) {

      $agenArr = [
        'name' => '店铺'
      ];
    }
    // $agenWhere = [
    //   'supplier_id' => $request->user->shop_id
    // ];
    // $agenArr = DB::table('supplier')->where($agenWhere)->select(['name', 'shop_id'])->get();
    $goodsWhere = [
      'shop_id' => $request->user->shop_id,
      'status' => 0
    ];
    $goodsArr = DB::table('goods')->where($goodsWhere)->select(['name', 'id'])->get();
    // var_dump($goodsArr);
    // 所有套餐
    // var_dump($agenArr);
    $agenArrName = []; // 代理名称
    $totle = 0;
    $arrayProduct = [];
    $arrayGoods = [];
    if (!empty($agenArr)) {
      $agenArr = json_decode(json_encode($agenArr), true);
      foreach ($agenArr as $key => $v) {
        $array = [];
        $array['name'] = $v['name'];
        // $array['prop'] = 'names' . $key;
        // $arrayProduct[] = $array;

        if (!empty($goodsArr)) {
          $goodsArr = json_decode(json_encode($goodsArr), true);

          $tileName1 = 0;
          foreach ($goodsArr as $k => $val) {
            // $arrayGoods = [];
            if ($key == 0) {
              $arrayProduct[] = [
                'label' => $val['name'],
                'prop' => 'names' . $k
              ];
            }

            $nums = DB::Table('order')->where($timeWhere)->where(function ($query) use ($val) {
              $query->where('goods_id', $val['id']);
              $query->OrWhere([['sup_goods_id_str', 'like', '%.' . $val['id'] . '.%']]);
            })->where(function ($actquery) use ($v, $shop_id) {
              $actquery->where('shop_id', $v['shop_id']);
              $actquery->OrWhere('shop_id_str', '%.' . $shop_id . '.' . $v['shop_id'] . '.%');
            })->count();
            $tileName1 += $nums;
            $array['names' . $k] = $nums;
            // DB::Table('order')->whereOr('goods_id',$val)->whereOr([['sup_goods_id_str','like',$val]])->whereOr()->count();
          }
          $array['name1'] = $tileName1;
          $totle += $tileName1;
          $arrayGoods[] = $array;
        }
      }
    }
    // var_dump($arrayProduct);
    // var_dump($arrayGoods);
    $data = [
      'product' => [
        ['label' => '代理名称', 'prop' => 'name'],
        ['label' => '合计', 'prop' => 'name1'],
      ],
      'datas' => $arrayGoods,
    ];
    $data['product'] = array_merge($data['product'], $arrayProduct);
    return $this->returnMsg(200, '查询成功', $data);
    $data = [
      'product' => [
        ['label' => '代理名称', 'prop' => 'name'],
        ['label' => '合计', 'prop' => 'name1'],
        ['label' => '产品1', 'prop' => 'name2'],
      ],
      'datas' => [
        ['name' => '商品1', 'name1' => 5, 'name2' => 6]
      ],
    ];
    return $this->returnMsg(200, '查询成功', $data);
  }

  // 七天产品数据

  public function getSevenDistAndProduct(Request $request)
  {
    $params = $request->all();
    // 时间计算
    // $one = date('Y-m-d', strtotime('-1 days'));
    // $two = date('Y-m-d', strtotime('-2 days'));
    // $three = date('Y-m-d', strtotime('-3 days'));
    // $fore = date('Y-m-d', strtotime('-4 days'));
    // $five = date('Y-m-d', strtotime('-5 days'));
    // $sex = date('Y-m-d', strtotime('-6 days'));
    // $seven = date('Y-m-d', strtotime('-7 days'));
    $one = $ones = date('Y-m-d', strtotime('-1 days'));
    $seven = $sevens = date('Y-m-d', strtotime('-7 days'));
    if (!empty($params['dateRange'])) {
      $seven = $sevens = $params['dateRange'][0];
      $one = $ones = $params['dateRange'][1];
    }
    $sevens = strtotime($sevens);
    $ones = strtotime($ones);
    $timesArr = [];
    while ($sevens <= $ones) {
      $timesArr[] = date('Y-m-d', $sevens);
      $sevens = $sevens + 86400;
    }

    $where[] = ['created', '>=', $seven];
    $where[] = ['created', '<=', $one];
    $where[] = ['status', '=', 200];
    // $where[] = ['nums', '>', 0];
    $datas = [];
    $act = $this->repoist->getAll($where, ['product_name', 'created', 'nums'], 'product_id');
    // var_dump($act, 11111111);
    // [
    //  ['产品名称'=>'a','nums'=> '2','created' => '3']
    //  ['产品名称'=> 'b','nums'=> ''2,'created'=>'2']
    //  ['产品名称'=>'c','nums'=> '3','created'=> '1']
    //]
    if (!empty($act)) {
      foreach ($act as $key => $v) {
        $datas[$v->product_name][] = ['nums' => $v->nums, 'created' => $v->created];
      }
    }
    $actDatas = [];
    $timesArrOne = array_unshift($timesArr, 'product');
    // var_dump($timesArr);
    $data = [
      $timesArr,
    ];
    // var_dump($data);
    if (!empty($datas)) {
      foreach ($datas as $key => $vs) {
        // $oneAct = '0';
        // $twoAct = '0';
        // $threeAct = '0';
        // $foreAct = '0';
        // $fiveAct = '0';
        // $sexAct = '0';
        // $sevenAct = '0';
        // $data[] = [$key];
        $actDatas = [];
        if (!empty($vs)) {
          foreach ($vs as $keys => $v) {

            foreach ($timesArr as $keyss => $val) {

              if ($val == 'product') {
                $actDatas[$keyss] = $key;
                continue;
              }
              if (!empty($actDatas[$keyss])) {
                continue;
              }
              if ($v['created'] == $val) {
                $actDatas[$keyss] = $v['nums'];
              } else {
                $actDatas[$keyss] = 0;
              }
            }
            // $actDatass = array_unshift($actDatas,$key);
            // switch ($v['created']) {
            //   case $one:
            //     $oneAct = $v['nums'];
            //     break;
            //   case $two:
            //     $twoAct = $v['nums'];
            //     break;
            //   case $three:
            //     $threeAct = $v['nums'];
            //     break;
            //   case $fore:
            //     $foreAct = $v['nums'];
            //     break;
            //   case $five:
            //     $fiveAct = $v['nums'];
            //     break;
            //   case $seven:
            //     $sevenAct = $v['nums'];
            //     break;
            //   case $sex:
            //     $sexAct = $v['nums'];
            //     break;
            // }
          }
        }
        $data[] = $actDatas;
      }
    }
    // $data = [
    //   ['product', $seven, $sex,  $five, $fore, $three, $two, $one],
    //   ['产品名称', $oneOrder, $twoOrder, $threeData, $foreData, $fiveData, $sexData, $sevenData], //订单数据
    //   ['发货数据', $oneDev, $twoDev, $threeDev, $foreDev, $fiveDev, $sexDev, $sevenDev], // 下单数据
    //   ['激活数据', $oneAct, $twoAct, $threeAct, $foreAct, $fiveAct, $sexAct, $sevenAct], // 下单数据
    // ];
    return $this->returnMsg(200, '查询成功', $data);
  }
  // 本月的统计   和 上月的统计
  public function staticAll(Request $request)
  {
    $params = $request->all();
    $shop_id = $request->user->shop_id; // 店铺id
    $dist_shop_id = $shop_id; // 代理id
    // 总单量
    // 激活单
    // 发货单
    //  上个月
    $startTimeUp = date('Y-m-01 00:00:00', strtotime('-1 month'));
    $day = date('d');
    $endTimeUp = date('Y-m-d 23:59:59', strtotime(-$day . ' day'));
    $startTime = date('Y-m-01 00:00:00');
    $endTime = date('Y-m-d H:i:s');
    $timeUpWhere = [
      ['created_at', '>=', $startTimeUp],
      ['created_at', '<=', $endTimeUp],
    ];
    $timeWhere = [
      ['created_at', '>=', $startTime],
      ['created_at', '<=', $endTime],
    ];
    $where = [
      'nums' => 1,  // 代表第一次提交
    ];
    //本月的
    $arrayData = DB::Table('orders')->select(DB::raw('count(*) as number,status,shop_id'))->where($where)->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
      $actquery->where('orders.shop_id', '=', $dist_shop_id);
      if ($dist_shop_id != $shop_id) {
        $actquery->OrWhere('orders.shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
      } else {
        $actquery->OrWhere('orders.shop_id_str', 'like',  $dist_shop_id . '.%');
      }
    })->groupBy(['status', 'shop_id'])->get();
    // 上个月的
    $arrayDataUp = DB::Table('orders')->select(DB::raw('count(*) as number,status,shop_id'))->where($timeUpWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
      $actquery->where('orders.shop_id', '=', $dist_shop_id);
      if ($dist_shop_id != $shop_id) {
        $actquery->OrWhere('orders.shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
      } else {
        $actquery->OrWhere('orders.shop_id_str', 'like',  $dist_shop_id . '.%');
      }
    })->groupBy(['status', 'shop_id'])->get();

    $arrayDataReturn = $this->upStaticStatus($arrayData, $shop_id);
    $arrayDataUpReturn = $this->upStaticStatus($arrayDataUp, $shop_id);
    return $this->returnMsg(200, '查询成功', ['datas' => $arrayDataReturn, 'upData' => $arrayDataUpReturn]);
  }
  // 今日的数据
  public function getTodayData(Request $request)
  {
    $params = $request->all();
    $shop_id = $request->user->shop_id; // 店铺id
    $dist_shop_id = $shop_id; // 代理id
    // 总单量
    // 激活单
    // 发货单
    $startTime = date('Y-m-d 00:00:00');
    $endTime = date('Y-m-d 23:59:59');
    $timeWhere = [
      ['created_at', '>=', $startTime],
      ['created_at', '<=', $endTime],
    ];
    $where = [
      'nums' => 1,  // 代表第一次提交
    ];
    //本月的
    $arrayData = DB::Table('orders')->select(DB::raw('count(*) as number,status,shop_id'))->where($where)->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
      $actquery->where('orders.shop_id', '=', $dist_shop_id);
      if ($dist_shop_id != $shop_id) {
        $actquery->OrWhere('orders.shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
      } else {
        $actquery->OrWhere('orders.shop_id_str', 'like',  $dist_shop_id . '.%');
      }
    })->groupBy(['status', 'shop_id'])->get();
    $arrayDataUpReturn = $this->upStaticStatus($arrayData, $shop_id);
    return $this->returnMsg(200, '查询成功', $arrayDataUpReturn);
  }
  // 根据状态处理
  public function upStaticStatus($data, $shop_id)
  {
    $return = [
      'totle' => 0,
      'agenTotle' => 0,
      'tsTotle' => 0,
      'ts' => [],
      'agen' => [],
    ];
    $model = new OrdersRepository();
    if (!empty($data)) {
      foreach ($data as $key => $v) {
        if ($v->shop_id == $shop_id) {
          $return['ts'][$v->status]['number'] = (!empty($return['ts'][$v->status]['number']) ? $return['ts'][$v->status]['number'] : 0) + $v->number;
          $return['tsTotle'] += $v->number;
        } else {
          $return['agenTotle'] += $v->number;
          $return['ts'][$v->status]['number2'] = (!empty($return['ts'][$v->status]['number2']) ? $return['ts'][$v->status]['number2'] : 0) +  $v->number;
        }
        $return['ts'][$v->status]['desc'] = $model->getOneOrderStatus($v->status);
        $return['totle'] += $v->number;
      }
    }
    return $return;
  }
  // 统计图表
  public function getChart(Request $request)
  {
    $params = $request->all();
    $where = [];
    $shop_id = $request->user->shop_id; // 店铺id
    // $shop_id = 19;
    $dist_shop_id = $shop_id; // 代理id
    if (!empty($params['shop_id'])) {
      $dist_shop_id = $params['shop_id'];
    }
    $time = date('Y-m-d');
    $startTime = date('Y-m-d 00:00:00');
    // $startTime = '2022-03-04 00:00:00';
    $endTime = date('Y-m-d 23:59:59');
    if (!empty($params['dateRange'])) {
      $time = $params['dateRange'];
      $startTime = $time[0];
      $endTime = $time[1];
    }
    $timeWhere = [];
    $timeWhere[] = [
      'orders.created_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
    ];
    $timeWhere[] = [
      'orders.created_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
    ];
    // 激活时间
    if (!empty($params['timeType']) && $params['timeType'] == 2) {
      $timeWhere = [];
      $timeWhere[] = [
        'orders.activat_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
      ];
      $timeWhere[] = [
        'orders.activat_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
      ];
    }
    // 发货时间
    if (!empty($params['timeType']) && $params['timeType'] == 1) {
      $timeWhere = [];
      $timeWhere[] = [
        'orders.deliver_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
      ];
      $timeWhere[] = [
        'orders.deliver_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
      ];
    }
    $whereIN = [];
    if (!empty($params['channel_id'])) {
      $whereIN[] = ['orders.sup_id', $params['channel_id']];
    }
    if (!empty($params['source'])) {
      $whereIN[] = ['orders_user.source', $params['source']];
    }
    if (!empty($params['goods_id'])) {
      $whereIN[] = ['orders.goods_id', $params['goods_id']];
    }
    $arrayGoods = [];
    // $arrayData = DB::Table('order')->select(DB::raw('count(*) as nums,status,goods_id,goods_name,goods_code'))->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
    //   $actquery->where('shop_id', '=', $dist_shop_id);
    //   if ($dist_shop_id != $shop_id) {
    //     $actquery->OrWhere('shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
    //   } else {
    //     $actquery->OrWhere('shop_id_str', 'like', '%.' . $dist_shop_id . '.%');
    //   }
    // })->groupBy('goods_id', 'goods_name', 'status', 'goods_code')->orderBy('goods_id', 'asc')->get();
    // DB::connection()->enableQueryLog();
    $model = DB::Table('orders')->select(DB::raw('count(*) as number,status,goods_id,goods_name,goods_code'));
    if (!empty($whereIN)) {
      foreach ($whereIN as $key => $v) {
        // var_dump($v);
        $model = $model->whereIn($v[0], $v[1]);
      }
    }
    $arrayData = $model->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
      $actquery->where('orders.shop_id', '=', $dist_shop_id);
      if ($dist_shop_id != $shop_id) {
        $actquery->OrWhere('orders.shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
      } else {
        $actquery->OrWhere('orders.shop_id_str', 'like',  $dist_shop_id . '.%');
      }
    })->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->groupBy('status', 'goods_name', 'goods_code', 'goods_id')->get();
    // 统计数据
    // $log = DB::getQueryLog();
    // var_dump($log);
    if (!empty($arrayData)) {
      // 重新组装数组
      $productArr = [];
      foreach ($arrayData as $key => $v) {
        // 数据转发
        $productArr[$v->goods_code][] = $v;
      }
    }
    $id = 0;
    if (!empty($productArr)) {
      $productArrRes = []; // 需要输入的数据
      if (!empty($productArr)) {
        foreach ($productArr as $key => $vval) {
          $arrays = [];
          $name = '';
          $totle = 0; // 总数
          $shen = 0;
          $active = 0;
          $send = 0;
          $failnum = 0;
          foreach ($vval as $ks => $vs) {
            $name = $vs->goods_name;
            if ($vs->status >= 0) {
              $totle += $vs->number;
            }
            if ($vs->status > 0 && $vs->status < 20) {
              $shen += $v->number;
            }
            if ($vs->status >= 20 && $vs->status <= 40) {
              $send += $v->number;
            }
            if ($vs->status == 40) {
              $active += $vs->number;
            }
            if ($vs->status > 40) {
              $failnum += $vs->number;
            }
          }
          // var_dump($arrays);
          $arrays = [
            $name,
            $totle,
            $shen,
            $send,
            $active,
            $failnum
          ];
          $arrayGoods[] = $arrays;
        }

        // $arrayGoods[] = $array;
      }
    }
    $arrayGoodsTitle[] = ['product', '总量', '审核', '发货', '激活', '失败'];
    $arrayGoods = array_merge($arrayGoodsTitle, $arrayGoods);
    // var_dump($arrayGoods, 11111111);
    // $keys = array_column($arrayGoods, 'name1');
    // array_multisort($arrayGoods, SORT_DESC, $keys);

    return $this->returnMsg(200, '查询成功', $arrayGoods);
  }
  // 统计饼图
  public function getPieChart(Request $request)
  {
    $params = $request->all();
    $where = [];
    $shop_id = $request->user->shop_id; // 店铺id
    // $shop_id = 19;
    $dist_shop_id = $shop_id; // 代理id
    if (!empty($params['shop_id'])) {
      $dist_shop_id = $params['shop_id'];
    }
    $time = date('Y-m-d');
    $startTime = date('Y-m-d 00:00:00');
    // $startTime = '2022-03-04 00:00:00'; // 测试使用
    $endTime = date('Y-m-d 23:59:59');
    if (!empty($params['dateRange'])) {
      $time = $params['dateRange'];
      $startTime = $time[0];
      $endTime = $time[1];
    }
    $timeWhere = [];
    $timeWhere[] = [
      'orders.created_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
    ];
    $timeWhere[] = [
      'orders.created_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
    ];
    // 激活时间
    if (!empty($params['timeType']) && $params['timeType'] == 2) {
      $timeWhere = [];
      $timeWhere[] = [
        'orders.activat_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
      ];
      $timeWhere[] = [
        'orders.activat_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
      ];
    }
    // 发货时间
    if (!empty($params['timeType']) && $params['timeType'] == 1) {
      $timeWhere = [];
      $timeWhere[] = [
        'orders.deliver_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
      ];
      $timeWhere[] = [
        'orders.deliver_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
      ];
    }
    $whereIN = [];
    if (!empty($params['channel_id'])) {
      $whereIN[] = ['orders.sup_id', $params['channel_id']];
    }
    if (!empty($params['source'])) {
      $whereIN[] = ['orders_user.source', $params['source']];
    }
    if (!empty($params['goods_id'])) {
      $whereIN[] = ['orders.goods_id', $params['goods_id']];
    }
    $arrayGoods = [];
    // $arrayData = DB::Table('order')->select(DB::raw('count(*) as nums,status,goods_id,goods_name,goods_code'))->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
    //   $actquery->where('shop_id', '=', $dist_shop_id);
    //   if ($dist_shop_id != $shop_id) {
    //     $actquery->OrWhere('shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
    //   } else {
    //     $actquery->OrWhere('shop_id_str', 'like', '%.' . $dist_shop_id . '.%');
    //   }
    // })->groupBy('goods_id', 'goods_name', 'status', 'goods_code')->orderBy('goods_id', 'asc')->get();
    // DB::connection()->enableQueryLog();
    $model = DB::Table('orders')->select(DB::raw('count(*) as number,goods_id,goods_name,goods_code'));
    if (!empty($whereIN)) {
      foreach ($whereIN as $key => $v) {
        // var_dump($v);
        $model = $model->whereIn($v[0], $v[1]);
      }
    }

    $arrayData = $model->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
      $actquery->where('orders.shop_id', '=', $dist_shop_id);
      if ($dist_shop_id != $shop_id) {
        $actquery->OrWhere('orders.shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
      } else {
        $actquery->OrWhere('orders.shop_id_str', 'like',  $dist_shop_id . '.%');
      }
    })->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->groupBy('goods_name', 'goods_code', 'goods_id')->get();
    // 统计数据
    // $log = DB::getQueryLog();
    // var_dump($log);
    $id = 0;
    if (!empty($arrayData)) {
      $productArrRes = []; // 需要输入的数据
      if (!empty($arrayData)) {
        foreach ($arrayData as $key => $vval) {
          $arrays = [];
          $name = $vval->goods_name;
          $totle = $vval->number; // 总数
          // var_dump($arrays);
          $arrays = [
            'name' => $name,
            'value' => $totle,
          ];
          $arrayGoods[] = $arrays;
        }

        // $arrayGoods[] = $array;
      }
    }
    // $arrayGoodsTitle[] = ['product', '总量', '审核', '发货', '激活', '失败'];
    // $arrayGoods = array_merge($arrayGoodsTitle, $arrayGoods);
    // var_dump($arrayGoods, 11111111);
    // $keys = array_column($arrayGoods, 'name1');
    // array_multisort($arrayGoods, SORT_DESC, $keys);

    return $this->returnMsg(200, '查询成功', $arrayGoods);
  }
  // 线性统计图 
  public function getLineChart(Request $request)
  {
    $params = $request->all();
    $where = [];
    $shop_id = $request->user->shop_id; // 店铺id
    // $shop_id = 19;
    $dist_shop_id = $shop_id; // 代理id
    if (!empty($params['shop_id'])) {
      $dist_shop_id = $params['shop_id'];
    }
    $time = date('Y-m-d');
    $startTime = date('Y-m-d 00:00:00');
    // $startTime = '2022-03-04 00:00:00';
    $endTime = date('Y-m-d 23:59:59');
    if (!empty($params['dateRange'])) {
      $time = $params['dateRange'];
      $startTime = $time[0];
      $endTime = $time[1];
    }
    $timeWhere = [];
    $timeWhere[] = [
      'orders.created_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
    ];
    $timeWhere[] = [
      'orders.created_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
    ];
    // 激活时间
    if (!empty($params['timeType']) && $params['timeType'] == 2) {
      $timeWhere = [];
      $timeWhere[] = [
        'orders.activat_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
      ];
      $timeWhere[] = [
        'orders.activat_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
      ];
    }
    // 发货时间
    if (!empty($params['timeType']) && $params['timeType'] == 1) {
      $timeWhere = [];
      $timeWhere[] = [
        'orders.deliver_at', '>=', date('Y-m-d H:i:s', strtotime($startTime)),
      ];
      $timeWhere[] = [
        'orders.deliver_at', '<=', date('Y-m-d H:i:s', strtotime($endTime)),
      ];
    }
    $whereIN = [];
    if (!empty($params['channel_id'])) {
      $whereIN[] = ['orders.sup_id', $params['channel_id']];
    }
    if (!empty($params['source'])) {
      $whereIN[] = ['orders_user.source', $params['source']];
    }
    if (!empty($params['goods_id'])) {
      $whereIN[] = ['orders.goods_id', $params['goods_id']];
    }
    $arrayGoods = [];
    // $arrayData = DB::Table('order')->select(DB::raw('count(*) as nums,status,goods_id,goods_name,goods_code'))->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
    //   $actquery->where('shop_id', '=', $dist_shop_id);
    //   if ($dist_shop_id != $shop_id) {
    //     $actquery->OrWhere('shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
    //   } else {
    //     $actquery->OrWhere('shop_id_str', 'like', '%.' . $dist_shop_id . '.%');
    //   }
    // })->groupBy('goods_id', 'goods_name', 'status', 'goods_code')->orderBy('goods_id', 'asc')->get();
    // DB::connection()->enableQueryLog();
    $model = DB::Table('orders')->select(DB::raw("count(*) as number,status,DATE_FORMAT(card_orders.created_at,'%Y-%m-%d') as day"));
    if (!empty($whereIN)) {
      foreach ($whereIN as $key => $v) {
        // var_dump($v);
        $model = $model->whereIn($v[0], $v[1]);
      }
    }

    $arrayData = $model->where($timeWhere)->where(function ($actquery) use ($dist_shop_id, $shop_id) {
      $actquery->where('orders.shop_id', '=', $dist_shop_id);
      if ($dist_shop_id != $shop_id) {
        $actquery->OrWhere('orders.shop_id_str', 'like', '%.' . $shop_id . '.' . $dist_shop_id . '.%');
      } else {
        $actquery->OrWhere('orders.shop_id_str', 'like',  $dist_shop_id . '.%');
      }
    })->leftJoin('orders_user', 'orders_user.id', '=', 'orders.orders_user_id')->groupBy('status', 'day')->get();
    // 统计数据
    // $log = DB::getQueryLog();
    // var_dump($log);
    $id = 0;
    if (!empty($arrayData)) {
      // 重新组装数组
      $productArr = [];
      foreach ($arrayData as $key => $v) {
        // 数据转发
        $productArr[$v->day][] = $v;
      }
    }
    $productArrRes = []; // 需要输入的数据
    if (!empty($productArr)) {

      if (!empty($productArr)) {
        foreach ($productArr as $key => $vval) {
          // 需要根据状态 计算
          $productArrRes['dateTime'][] = $key;
          $actnum = 0;
          $totleNum = 0;
          $failNum = 0;
          foreach ($vval as $keys => $v) {
            $totleNum += $v->number;
            if ($v->status == 40) {
              $actnum += $v->number;
            }
            if ($v->status > 40) {
              $failNum = $v->number;
            }
          }
          $productArrRes['expectedData'][] = $totleNum;
          $productArrRes['fialData'][] = $failNum;
          $productArrRes['actualData'][] = $actnum;
        }

        // $arrayGoods[] = $array;
      }
    }
    // $arrayGoodsTitle[] = ['product', '总量', '审核', '发货', '激活', '失败'];
    // $arrayGoods = array_merge($arrayGoodsTitle, $arrayGoods);
    // var_dump($arrayGoods, 11111111);
    // $keys = array_column($arrayGoods, 'name1');
    // array_multisort($arrayGoods, SORT_DESC, $keys);
    // return '{"code":200,"msg":"\u67e5\u8be2\u6210\u529f","data":{"dateTime":["2023-04-15","2023-04-16","2023-04-17","2023-04-18"],"expectedData":[29,32,19,34],"fialData":[0,0,0,11],"actualData":[0,0,0,0]}}';
    return $this->returnMsg(200, '查询成功', $productArrRes);
  }
}
